package com.cardone.common.security.shiro.session.mgt.eis;

import com.cardone.common.cache.util.CacheUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;

import java.io.Serializable;
import java.util.Collection;

/**
 * Created by Administrator on 2014/11/10.
 */
@Slf4j
public class CardOneSessionDao extends AbstractSessionDAO {
    @Override
    protected Serializable doCreate(Session session) {
        super.assignSessionId(session, super.generateSessionId(session));

        this.update(session);

        return session.getId();
    }

    @Override
    protected Session doReadSession(Serializable sessionId) {
        return CacheUtils.get(Session.class, CardOneSessionDao.class.getName(), sessionId);
    }

    @Override
    public void update(Session session) throws UnknownSessionException {
        CacheUtils.put(CardOneSessionDao.class.getName(), session.getId(), session);
    }

    @Override
    public void delete(Session session) {
        CacheUtils.evict(CardOneSessionDao.class.getName(), session.getId());
    }

    @Override
    public Collection<Session> getActiveSessions() {
        return CacheUtils.values(CardOneSessionDao.class.getName());
    }
}
